﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Zjs_Shop.Models;
using Zjs_Shop.Models.Database;
using Zjs_Shop.Service;

namespace Zjs_Shop.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        //Scaffold-DbContext "Data Source=.;Initial Catalog=Zjs-Shop;Integrated Security=SSPI" Microsoft.EntityFrameworkCore.SqlServer -o ./Models/Database -Force 数据库连接
        //定义数据库上下文
        private readonly ZjsShopContext _db;
        //注入jwt服务
        private readonly IJwtService _jwtService;
        //注入用户服务服务
        private readonly IUserService _userService;
        //注入Redis服务
        //private readonly IRedisService _redisService;

        private readonly IUsers _users;
        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        public HomeController(ZjsShopContext db, IJwtService jwtService,IUserService userService,IUsers users)
        {
            _db = db;
            _jwtService = jwtService;
            _userService = userService;
           
            _users = users;
        }
        /// <summary>
        /// 创建token
        /// </summary>
        /// <returns></returns>
        //[HttpGet]
        //public string CreateToken(string phone)
        //{
        //    return _jwtService.CreateToken(phone);
        //}
        [HttpGet]
        [Authorize]
        public string UserInfo()
        {
            //获取用户信息
            return Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
        }
        //编写接口完成注册功能，保证用户名唯一
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string Reg(RegRequest request)
        {

            //使用服务
            if (_userService.TfUserphone(request.Phone))
                return "用户存在";


            var UserId = _userService.Reg(request.Phone, request.Password);
            if (UserId > 0)
            {
                return "注册成功";
            }
            return "注册失败";
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public string Login(RegRequest request)
        {
           
            var user = _userService.GetphoneByphone(request.Phone);
            if (user == null)
                return "用户不存在";

            //生成md5字符串                      输入的         数据库的盐值
            var md5 = MD5Helper.MD5Encrypt32(request.Password + user.Salt.ToString());
            if (user.Password != md5)
                return "密码不正确";

            return _jwtService.CreateToken(user.Id,user.Phone);
            
        }
        /// <summary>
        /// 个人中心
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public List<UserCenter> UserCenter()
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.UserCenter(Convert.ToInt32(Id));

        }
        /// <summary>
        /// 修改个人中心
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Username"></param>
        /// <param name="Email"></param>
        /// <param name="Address"></param>
        /// <returns></returns>

        [Authorize]
        [HttpPost]
        
        public string Updatauser( string Username, string Photo, string Address)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            Console.WriteLine(Id);
            return _userService.Updatauser(Convert.ToInt32(Id),Username, Photo, Address);
        }
        /// <summary>
        /// 新增商品
        /// </summary>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="description"></param>
        /// <param name="category_id"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddProduct(string name, decimal price, string description, int category_id, int stock)
        {
            var productid = _userService.Addpro(name, price, description, category_id,stock);
            if (productid > 0)
            {
                return "新增成功";
            }
            return "新增失败";
        }
        /// <summary>
        /// 根据ID更新商品数据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="name"></param>
        /// <param name="price"></param>
        /// <param name="description"></param>
        /// <param name="category_id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Updateproduct(int Id, string name, decimal price, string description, int category_id, int stock)
        {
            return _userService.Updatepro(Id, name, price, description, category_id,stock);
        }
        /// <summary>
        /// 根据商品ID查询
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public Product GetProducts(int  Id)
        {
            return _userService.GetProducts(Id);
        }
        /// <summary>
        /// 根据id删除商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string DelProduct(int id)
        {
            return _userService.DelProduct(id);
        }
        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="CategoryId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory(int CategoryId, string name)
        {
            return _userService.AddCategory(CategoryId, name);
        }
        /// <summary>
        /// 更新分类数据
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="CategoryId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateCategory(int Id, int CategoryId, string name)
        {
            return _userService.UpdateCategory(Id,CategoryId, name);
        }
        /// <summary>
        /// 查询分类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        public Category GetCategory(int Id)
        {
            return _userService.GetCategory(Id);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string DelCategory(int id)
        {
            return _userService.DelCategory(id);
        }
        [HttpPost]
        public GetProResponse GetGoodShow(int CateId, string Keyword, int PageIndex, int PageSize)
        {
            return _userService.GetGoodShow(CateId,Keyword,PageIndex,PageSize);
        }
        /// <summary>
        /// 添加购物车商品id 数量自增1
        /// </summary>
        /// <param name="product_id"></param>
        /// <param name="quantity"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddCar(int product_id)
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.AddCar(Convert.ToInt32(Id), product_id);
           // return  _userService.UpdataACar(product_id);
            
        }
        /// <summary>
        /// 减少购物车
        /// </summary>
        /// <param name="product_id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string UpdataCar(int product_id)
        {
            return _userService.UpdataCar(product_id);
        }
        /// <summary>
        /// 删除购物车
        /// </summary>
        /// <param name="product_id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string DelCar(int product_id)
        {
            return _userService.DelCar(product_id);
        }
        /// <summary>
        /// 查询购物车
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public List<ModelCar> Getcar()
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.Getcar(Convert.ToInt32(Id));
        }
        //[Authorize]
        //[HttpPost]
        //public List<ModelOrder> modelOrders()
        //{
        //    var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
        //    return _userService.modelOrders(Convert.ToInt32(Id));
        //}
        /// <summary>
        /// 结算 生成订单和订单详细
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public string AddOrder()
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.AddOrder(Convert.ToInt32(Id));
             
        }
        /// <summary>
        /// 用户查询订单状态
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public List<ModelOrder> modelOrders()
        {
            var Id = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            return _userService.modelOrders(Convert.ToInt32(Id));
        }
        /// <summary>
        /// 后台查询订单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<ModelBsg> ModelBsg()
        {
            return _userService.ModelBsg();
        }
        /// <summary>
        /// 后台发货
        /// </summary>
        /// <param name="id"></param>
        /// <param name="expcom"></param>
        /// <param name="expressid"></param>
        /// <returns></returns>
        [HttpPost]
        public string BsgSend(int id, string expcom, string expressid)
        {
            return _userService.BsgSend(id, expcom, expressid);
        }
    }
}
